﻿package lobby
{
	import com.sickworks.components.DateChooserComp;
	import fl.controls.TextInput;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Transform;
	import flash.text.TextField;
	import fl.events.ComponentEvent;
	import fl.controls.ComboBox;
	import fl.data.DataProvider; 
	import lobby.exchangeddata.BetStatusInfo;
	import lobby.events.LobbyEvent;
	import lobby.exchangeddata.HashTable;
	import lobby.common.Utility;
	import lobby.constants.ConstData;
	import lobby.exchangeddata.TransferItemInfo;
	import lobby.exchangeddata.TransferReportInfo;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class TransferReportPanel extends MovieClip
	{
		public var dateChoose:DateChooserComp;
		public var dateFromIcon:MovieClip;
		public var dateToIcon:MovieClip;
		public var txtDateFromTi:TextField;
		public var txtDateToTi:TextField;
		public var btnSearchReport:SimpleButton;
		
		public var txtPastTf:TextField;
		public var txtOkTf:TextField
		public var txtDateFrom:TextField;
		public var txtDateTo:TextField;
		public var txtSearch:TextField;
			
		public var title_txt:TextField;
		public var titleRowNo:TextField;
		public var titleGameSiteCol:TextField;
		public var titleBetNo:TextField;
		public var titleDate:TextField;
		public var titleHostAmount:TextField;
		public var titleHostDeposit:TextField;
		public var titleHostWithdraw:TextField;
		public var titleHostBalance:TextField;
		public var titleGameSiteAmount:TextField;
		public var titleGameSiteDeposit:TextField;		
		public var titleGameSiteWithdraw:TextField;
		public var titleGameSiteBalance:TextField;
		public var titleFromDate:TextField;
		public var titleToDate:TextField;
		public var titleGameSite:TextField;
		
		public var txttotal:TextField;
		public var totalDeposit:TextField;
		public var totalWithDraw:TextField;
		
		public var txtReportMessageTf:TextField;
		
		
		public var btnCloseReport:SimpleButton;
		
		public var txtTransactionTotalAmountTf:TextField;
		
		public var transactionMc:MovieClip;
		public var hisBoard:Sprite = new Sprite();
		public var pageControl:PageControl;
		public var transactionDetail:TransactionDetailPanel;
		
		private var isAddPageControl:Boolean = true;
		
		private var currentIconDate:MovieClip;
		private var dateFrom:Array;
		private var dateTo:Array;
		public var cboGameSite:ComboBox;
		public function TransferReportPanel()
		{
			hide();
			
			dateFrom = new Array();
			dateTo = new Array();
			this.dateChoose.visible = false;
			
			this.txtOkTf.mouseEnabled = false;
			this.txtSearch.mouseEnabled = false;
			
			this.dateFromIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateToIcon.addEventListener(MouseEvent.CLICK, dateIconClickHdl);
			this.dateChoose.addEventListener(DateChooserComp.SELECTION_CHANGED, dateChooseClickHdl);
			this.btnSearchReport.addEventListener(MouseEvent.CLICK, searchReportHdl);
			
			this.btnCloseReport.addEventListener(MouseEvent.CLICK, closeHdl);
			
			transactionMc.addChild(hisBoard);
		}
		
		private function closeHdl(e:Event):void
		{
			hide();
		}
		
		private function searchReportHdl(e:Event):void
		{
			if (txtDateFromTi.text == "" || txtDateToTi.text == "")
			{
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_DATE_NULL, true));
				return;
			}
			var dateF:Date = new Date(dateFrom[0], dateFrom[1], dateFrom[2]);
			var dateT:Date = new Date(dateTo[0], dateTo[1], dateTo[2]);
			if (dateF < dateT)
			{
				this.isAddPageControl = true;
				var event = new LobbyEvent(LobbyEvent.GET_TRANSACTION_REPORT, true);
				event.ms = 1;
				dispatchEvent(event);
			}
			else
			{
				dispatchEvent(new LobbyEvent(LobbyEvent.SHOW_ERR_CHOOSE_DATE, true));
			}
		}
		
		private function dateChooseClickHdl(e:Event):void
		{
			trace("choose");
			this.dateChoose.visible = false;
			var dateSelect:String = e.target.selectedDates[0];
			var arrayDate:Array = dateSelect.split(" ");
			dateSelect = arrayDate[2] + "/" + arrayDate[1] + "/" + arrayDate[5];
			if (currentIconDate == dateToIcon)
			{
				txtDateToTi.text = dateSelect;
				dateTo[0] = arrayDate[5];
				dateTo[1] = Utility.getMonthNumber(arrayDate[1]);
				dateTo[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_TO_TRANSACTION, true));
			}
			else if (currentIconDate == dateFromIcon)
			{
				txtDateFromTi.text = dateSelect;
				dateFrom[0] = arrayDate[5];
				dateFrom[1] = Utility.getMonthNumber(arrayDate[1]);
				dateFrom[2] = arrayDate[2];
				dispatchEvent(new LobbyEvent(LobbyEvent.UPDATE_DATE_FROM_TRANSACTION, true));
			}
		}
		
		private function dateIconClickHdl(e:MouseEvent):void
		{
			if (e.currentTarget == dateToIcon)
			{
				this.dateChoose.x = this.dateToIcon.x - 20;
				this.dateChoose.visible = true;
				setChildIndex(this.dateChoose, this.numChildren - 1);
				currentIconDate = dateToIcon;
			}
			else if (e.currentTarget == dateFromIcon)
			{
				this.dateChoose.x = this.dateFromIcon.x - 20;
				this.dateChoose.visible = true;
				setChildIndex(this.dateChoose, this.numChildren - 1);
				currentIconDate = dateFromIcon;
			}
		}
		
		public function updateDate(dFrom:String, dTo:String):void
		{
			trace("vo updateDate");
			this.txtDateFromTi.text = dFrom;
			this.txtDateToTi.text = dTo;
			var tempFrom:Array = dFrom.split("/");
			this.dateFrom[0] = tempFrom[2];
			this.dateFrom[1] = Utility.getMonthNumber(tempFrom[1]);
			this.dateFrom[2] = tempFrom[0];
			
			var tempTo:Array = dTo.split("/");
			this.dateTo[0] = tempTo[2];
			this.dateTo[1] = Utility.getMonthNumber(tempTo[1]);
			this.dateTo[2] = tempTo[0];
			
			var listVen:Array = new Array();
			var listGameSite:Array = new Array();
			var tmp:Array;
			trace("GlobalVariables.listDepWithVen-------- : " + GlobalVariables.listDepWithVen);
			listVen = GlobalVariables.listDepWithVen.split("&");
			listGameSite.push({data:-1, label:"All"});
			for (var i=0; i<listVen.length; i++) {
				tmp = new Array();
				tmp = listVen[i].split("*");
				listGameSite.push({data:tmp[0], label:tmp[1]});
			}
			cboGameSite.dataProvider = new DataProvider(listGameSite);
			cboGameSite.addEventListener(Event.CHANGE, changeHandler);
		}
		
		private function changeHandler(ev:Event):void
		{
			GlobalVariables.vendorId = ev.currentTarget.selectedItem.data;
			trace(ev.currentTarget.selectedItem.data);
		} 
		public function initText(hashTableInfo:HashTable):void
		{
			trace("vo inittext");
			this.txtPastTf.text = hashTableInfo.lookupString("depositwithdrawform");
			this.titleRowNo.text = hashTableInfo.lookupString("titleRowNo");
			this.titleGameSiteCol.text = hashTableInfo.lookupString("titleGameSiteCol");
			this.titleBetNo.text = hashTableInfo.lookupString("titleBetNo");
			this.titleDate.text = hashTableInfo.lookupString("titleDate");
			this.titleHostAmount.text = hashTableInfo.lookupString("titleHostAmount");
			
			this.titleHostDeposit.text = hashTableInfo.lookupString("titleDeposit");
			this.titleHostWithdraw.text = hashTableInfo.lookupString("titleWithdraw");
			this.titleHostBalance.text = hashTableInfo.lookupString("titleBalance");
			this.titleGameSiteAmount.text = hashTableInfo.lookupString("titleGameSiteAmout");
			this.titleGameSiteDeposit.text = hashTableInfo.lookupString("titleDeposit");
			this.titleGameSiteWithdraw.text = hashTableInfo.lookupString("titleWithdraw");
			this.titleGameSiteBalance.text = hashTableInfo.lookupString("titleBalance");
			
			this.txtDateFrom.text = hashTableInfo.lookupString("titleFromDate");
			this.txtDateTo.text = hashTableInfo.lookupString("titleToDate");
			this.titleGameSite.text = hashTableInfo.lookupString("titleGameSite");
			this.txtOkTf.text = hashTableInfo.lookupString("btnclose");
			this.txtSearch.text = hashTableInfo.lookupString("btnSubmit");
		}
		
		public function updateValueTransferReport(info:Object):void
		{
			trace("updateValueTransferReport");
			
			totalDeposit.text = GlobalVariables.totalDeposit;
			totalWithDraw.text = GlobalVariables.totalWithDraw;
			var transferReport:TransferReportInfo = TransferReportInfo(info);
			removeHistory();
			for (var i:int = 0; i < transferReport.dataTransation.length; i++)
			{
				
				var itemData:TransferItemInfo = transferReport.dataTransation[i];
				var itemMc:TransferItem = new TransferItem(itemData);
				itemMc.x = 0;
				itemMc.y = i * 21 + i;
				hisBoard.addChild(itemMc);
			}
			if (this.isAddPageControl == true)
			{
				if (transferReport.pageNum > 1)
				{
					this.pageControl.addPageToPageList(this.pageControl.pageList, transferReport.pageNum, ConstData.PAGE_CONTROL_TRANSACTION_REPORT);
					this.pageControl.visible = true;
				}
			}
			this.isAddPageControl = false;
		}
		
		public function show():void
		{
			this.visible = true;
		}
		
		public function hide():void
		{
			this.visible = false;
			this.pageControl.visible = false;
		}
		
		private function removeHistory():void
		{
			
			var numberChid:int = hisBoard.numChildren;
			if (numberChid > 0)
			{
				for (var j:int = 0; j < numberChid; j++)
				{
					var his:DisplayObject = hisBoard.removeChildAt(0);
					his = null;
				}
			}
		}
		
		public function updateValueDetailTransactionReport(info:Object):void
		{
			trace("updateValueDetailTransactionReport");
			this.transactionDetail.updateValueTransactionReport(info);
		}
	}

}